import MainController from "./MainController";
import Config from "./Config";
import BallMotionStreak from "./BallMotionStreak";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Ball extends cc.Component {

    // 
    rigidBody: cc.RigidBody = null;

    isTouchedGround: boolean = false;
    main: MainController = null;
    onLoad() {
        // 给缸体赋值
        this.rigidBody = this.getComponent(cc.RigidBody);
    }

    start() {


    }

    update(dt: number) {
        if (this.isTouchedGround) {
            this.rigidBody.active = false;
            this.rigidBody.linearVelocity = cc.Vec2.ZERO;

            let pathPos: cc.Vec2[] = [];
            pathPos.push(this.node.position);
            pathPos.push(cc.v2(198, -499));
            pathPos.push(cc.v2(330, -473));
            pathPos.push(cc.v2(330, 595));
            pathPos.push(cc.v2(113, 528));
            
            this.node.runAction(cc.sequence(cc.cardinalSplineTo(2, pathPos, 1), cc.callFunc(function () {
                this.rigidBody.active = true;
                this.node.ground = Config.groupBallIngRecycle;
                this.main.recycleBall();
            }.bind(this))));
            this.isTouchedGround = false;
        }
    }


    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {

    }
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
        if (otherCollider.node.name == "ground") {
            this.isTouchedGround = true;
        }
    }

    onPreSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {
    }
    onPostSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider): void {

    }
}
